﻿using System.Collections.Generic;
using System.Linq;
using Omu.AwesomeDemo.Core.Model;
using Omu.ValueInjecter;

namespace Omu.AwesomeDemo.Infra.Builder
{
    public class EntitiesToInts : ConventionInjection
    {
        protected override bool Match(ConventionInfo c)
        {
            return c.SourceProp.Name == c.TargetProp.Name
                   && c.SourceProp.Type.IsGenericType
                   && c.TargetProp.Type.IsGenericType
                   && c.SourceProp.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>)
                   && c.TargetProp.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>)
                   && c.SourceProp.Type.GetGenericArguments()[0].IsSubclassOf(typeof(Entity))
                   && c.TargetProp.Type.GetGenericArguments()[0] == typeof(int);
        }

        protected override object SetValue(ConventionInfo c)
        {
            if (c.SourceProp.Value == null) return null;
            return (c.SourceProp.Value as IEnumerable<Entity>).Select(o => o.Id);
        }
    }
}